package com.wkhyapp.lm.view;

import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.lidong.photopicker.PhotoPickerActivity;
import com.lidong.photopicker.SelectModel;
import com.lidong.photopicker.intent.PhotoPickerIntent;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.base.SuperActivity;
import com.wkhyapp.lm.contract.FeedbackPresenter;
import com.wkhyapp.lm.contract.FeedbackView;
import com.wkhyapp.lm.http.net.AddGoodsRequest;
import com.wkhyapp.lm.http.vo.Member;
import com.wkhyapp.lm.utils.IdUtils;
import com.wkhyapp.lm.utils.MemberUtils;
import com.wkhyapp.lm.utils.StatusBarFontUtil;

import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;

public class FeedbackActivity extends SuperActivity<FeedbackPresenter> implements FeedbackView {

    @BindView(R.id.back_iv)
    ImageView back_iv;
    @BindView(R.id.submit_but)
    Button submit_but;
    @BindView(R.id.desc_et)
    EditText desc_et;
    @BindView(R.id.phone_et)
    EditText phone_et;
    @BindView(R.id.weixin_et)
    EditText weixin_et;
    @BindView(R.id.shangjia_et)
    EditText shangjia_et;
    @BindView(R.id.upload_iv)
    ImageView upload_iv;
    String imgurl;
    private static final int REQUEST_CAMERA_CODE = 10;
    private static final int REQUEST_PREVIEW_CODE = 20;
    private ArrayList<String> imagePaths = new ArrayList<>();
    private ArrayList<String> imgList = new ArrayList<>();
    private String patch;
    @Override
    public void initUI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            translucentStatusBar();
        }
        StatusBarFontUtil.setLightStatusBarColor(this);

    }

    @Override
    protected void initListener() {
        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        submit_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("".equals(desc_et.getText().toString())){
                    TToast("请输入投诉或建议的内容");
                    return;
                }
                if("".equals(phone_et.getText().toString())){
                    TToast("请输入手机号");
                    return;
                }
                if("".equals(weixin_et.getText().toString())){
                    TToast("请输入微信号");
                    return;
                }
                showLoad("正在提交...");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dismissLoad();
                        TToast("感谢您的反馈,我们已收到。");
                        finish();
                    }
                },2000);

            }
        });

        upload_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoPickerIntent intent = new PhotoPickerIntent(FeedbackActivity.this);
                intent.setSelectModel(SelectModel.MULTI);
                intent.setShowCarema(false); // 是否显示拍照
                intent.setMaxTotal(1); // 最多选择照片数量，默认为6
                intent.setSelectedPaths(imagePaths); // 已选中的照片地址， 用于回显选中状态
                startActivityForResult(intent, REQUEST_CAMERA_CODE);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                // 选择照片
                case REQUEST_CAMERA_CODE:
                    ArrayList<String> list = data.getStringArrayListExtra(PhotoPickerActivity.EXTRA_RESULT);
                    Log.d(TAG, "数量：" + list.size());
                    patch = list.get(0);
                    Glide.with(FeedbackActivity.this)
                            .load(list.get(0))
                            .placeholder(R.drawable.default_error)
                            .error(R.drawable.default_error)
                            .centerCrop()
                            .crossFade()
                            .into(upload_iv);
                    break;
            }
        }
    }


    @Override
    protected void afterCreate() {

    }

    @Override
    public void setSucc(String str) {
        dismissLoad();
        TToast("我们已收到,感谢您的反馈");
        finish();
    }



    @Override
    public void setToken(String token) {
        uploadImg2QiNiu(imgurl, MemberUtils.getUid()+"",token);
    }

    Configuration config = new Configuration.Builder().build();
    UploadManager uploadManager = new UploadManager(config);
    private void uploadImg2QiNiu(String data,String userId,String token) {
        String key = userId+"_"+System.currentTimeMillis()+"_"+ IdUtils.getShartUUID()+".jpg";
        Log.i("token", token);
        uploadManager.put(data, key, token,
                new UpCompletionHandler() {
                    @Override
                    public void complete(String key, ResponseInfo info, JSONObject res) {
                        if(info.isOK()) {
                            Log.i("qiniu", "Upload Success");
                        imgurl = key;
                            mPresenter.submit(shangjia_et.getText().toString(),desc_et.getText().toString(),
                                    weixin_et.getText().toString(),phone_et.getText().toString(),imgurl);
                        } else {
                            Log.i("qiniu", "Upload Fail");
                        }
                    }
                }, null);
     }


    @Override
    protected FeedbackPresenter createPresenter() {
        return mPresenter = new FeedbackPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_feedback;
    }


}
